//
//					Simple "Hello, world!" application, that uses 
//						NeWA library

#include "Application.h"
#include "Disposals.h"
#include "Button.h"
#include "Label.h"
#include "Frame.h"
#include "StyleSimpleAgg.h"
#include "Layout.h"

#include <sstream>

using namespace newa;

Loki::SmartPtr<newa::Frame> frame( new newa::Frame );
Loki::SmartPtr<newa::Button> button( new newa::Button );
Loki::SmartPtr<newa::Label> label( new newa::Label );

Loki::SmartPtr<newa::Button> button2( new newa::Button );


void changeLabel()
{
	static int n = 0;
	n++;

	if( (n & 1) == 0) {
		newa::Settle< newa::Disposals::WinOwner >( label->elem(), button->site() );
		label->rect( newa::Rect( 10, 5, 250, 30 ) );

		std::wstringstream sstr;
		sstr << L"Hello, world! (" << n << L")";
		label->text( sstr.str() );
	} else {
		newa::Settle< newa::Disposals::WinOwner >( label->elem(), newa::Screen() /*button->site()*/ );
		label->rect( newa::Rect( 100, 500, 250, 30 ) );
	}
}

typedef newa::Disposals::WinLessee ButtonDisposal;
typedef newa::Disposals::WinOwner LabelDisposal;

Layouts::Align align( Layouts::TOP );

void changeButton()
{
	static int n = 0;
	n++;

	if( (n & 1) == 0) {
		newa::Settle< ButtonDisposal >( button->elem(), frame->site(), 30, 50 );
		align.manage( *button->elem() );
	} else {
		align.unmanage( *button->elem() );
		newa::Settle< newa::Disposals::WinOwner >( button->elem(), newa::Screen(), 20, 500 );
	}
}

void button2Clicked()
{
	static bool f = false;
	static newa::WidgetElementRef OldSite;
	f = !f;
/*
	if( f ) {
		OldSite = button->elem()->parent();
		newa::Settle< newa::Disposals::Void >( button->elem(), newa::Screen() );
	} else {
		newa::Settle< ButtonDisposal >( button->elem(), OldSite, 30, 50 );
	}
*/
	if( f ) {
		newa::Settle< newa::Disposals::WinLessee >( label->elem(), button2->site(), 10, 5 );
	} else {
		newa::Settle< newa::Disposals::WinLessee >( label->elem(), button->site(), 10, 5 );
	}
}

int newa_test1()
{
	newa::styleMan().applyStyle( &newa::SimpleAggStyle() );

	newa::Settle< newa::Disposals::WinOwner > ( frame->elem(), newa::Screen() );
	frame->rect( newa::Rect( 10, 100, 320, 240) );

	newa::Settle< ButtonDisposal >( button->elem(), frame->site() );
	button->OnClick = changeButton;
	button->rect( newa::Rect( 30, 50, 270, 40 ) );
	
	label->text( _T("Hello, world!") );
	newa::Settle< LabelDisposal >( label->elem(), button->site() );
	label->rect( newa::Rect( 10, 5, 250, 30 ) );

	newa::Settle< newa::Disposals::WinLessee >( button2->elem(), frame->site() );
	button2->rect( newa::Rect( 30, 100, 270, 40 ) );
	button2->OnClick = button2Clicked;

	align.listen( *frame->site() );
	align.manage( *button->elem() );
	//align.layout();

	return newa::appInstance().run();
}

int newa_layoutTest()
{
	newa::styleMan().applyStyle( &newa::SimpleAggStyle() );

	newa::Settle< newa::Disposals::WinOwner > ( frame->elem(), newa::Screen() );
	frame->rect( newa::Rect( 10, 100, 320, 240) );

	newa::Settle< ButtonDisposal >( button->elem(), frame->site() );
	button->rect( newa::Rect( 30, 50, 270, 40 ) );

	//Layouts::Align align ( Layouts::TOP );
	align.listen( *frame->site() );
	align.manage( *button->elem() );
	
	return newa::appInstance().run();
}
/*
int newa_main()
{
	return newa_test1();
	//return newa_layoutTest();
}
*/